/**
 *    Copyright 2012 Geensoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package ar.com.greensoft.neo.dao.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import ar.com.greensoft.neo.dao.DaoException;
import ar.com.greensoft.neo.dao.PaisDao;
import ar.com.greensoft.neo.model.Pais;
import ar.com.greensoft.neo.model.Pais_;
import ar.com.greensoft.neo.model.references.ISO3166_1;
import ar.com.greensoft.neo.search.SearchInput;
import ar.com.greensoft.neo.search.TextSearchInput;
import ar.com.greensoft.neo.util.SearchUtil;

/**
 * 
 * @author Jorge E. Villaverde
 * @since 25/01/2012
 * @version 1.0.0
 */
@Transactional
@Repository
public class JpaPaisDaoImpl extends AbstractJpaWithRevisionDaoImpl<Pais> implements PaisDao {

	@Override
	public List<Pais> findLikeName(String name) {
		if(logger.isTraceEnabled())
			logger.trace("Returning Pais Line Name: " + name);

		name = SearchUtil.likeString(name); 
		
		TypedQuery<Pais> query = getEntityManager().createNamedQuery(Pais.FIND_LIKE_NAME_QUERY_NAME, Pais.class);

		query.setParameter("name", name);
		
		return query.getResultList();
	}

	@Override
	public Pais findByCode(ISO3166_1 codigoISO) throws DaoException {
		if(logger.isTraceEnabled())
			logger.trace("Returning Pais with ISO Code: " + codigoISO.toString());

		TypedQuery<Pais> query = getEntityManager().createNamedQuery(Pais.FIND_BY_ISO_QUERY_NAME, Pais.class);

		query.setParameter("codigoISO", codigoISO);
		
		return getSingleResult(query);
	}

	@Override
	protected Class<Pais> getEntityClazz() {
		return Pais.class;
	}

	@Override
	protected String getCountNamedQuery() {
		return Pais.COUNT_QUERY_NAME;
	}

	@Override
	protected String getListNamedQuery() {
		return Pais.FIND_ALL_QUERY_NAME;
	}

	@Override
	protected Expression<Boolean> createWhereFromSearchInput(SearchInput input, Root<Pais> root) {
		TextSearchInput tsi = (TextSearchInput)input;
		
		if(tsi == null)
			return null;
		if(!StringUtils.hasText(tsi.getText()))		
			return null;

		String searchText = SearchUtil.likeString(tsi.getText());
		CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
		
		Predicate predicate = criteriaBuilder.like(
				root.get(Pais_.name), searchText);
		
		return predicate;
	}
}
